package com.cookor.poi.modules.mx.api;

import com.cookor.poi.common.utils.IdGen;
import com.cookor.poi.common.utils.ResultUtil;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.modules.mx.dao.MxSfjDao;
import com.cookor.poi.modules.mx.entity.MxSfj;
import com.cookor.poi.modules.mx.entity.TMxOrder;
import com.cookor.poi.modules.mx.service.MxSfjService;
import com.cookor.poi.modules.mx.service.TMxOrderService;
import com.cookor.poi.modules.mx.util.MD5Util;

import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({"/mx"})
public class MxApiController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private TMxOrderService tMxOrderService;

    @Autowired
    private MxSfjService mxSfjService;

    @Autowired
    private MxSfjDao mxSfjDao;

    @RequestMapping({"/addOrder"})
    @ResponseBody
    public ResultUtil addOrder(String userName, String mobile, HttpServletRequest request) {
        ResultUtil resultUtil = new ResultUtil(ResultUtil.SUCCESS_CODE);
        try {
            String apiId = (request.getHeader("apiId") == null) ? "" : request.getHeader("apiId");
            String salt = (request.getHeader("apiSalt") == null) ? "" : request.getHeader("apiSalt");
            String sign = (request.getHeader("apiSign") == null) ? "" : request.getHeader("apiSign");
            if (StringUtils.isBlank(apiId)) {
                resultUtil.setCode("101");
                resultUtil.setError("没有接口访问权限，缺少识别号请求头");
                return resultUtil;
            }
            if (StringUtils.isBlank(salt)) {
                resultUtil.setCode("101");
                resultUtil.setError("没有接口访问权限，缺少随机数请求头");
                return resultUtil;
            }
            if (StringUtils.isBlank(sign)) {
                resultUtil.setCode("101");
                resultUtil.setError("没有接口访问权限，缺少签名请求头");
                return resultUtil;
            }
            if (!MD5Util.verify(apiId, salt, "9wjofm8vzw0c0tz4i9z5h1cs", sign)) {
                resultUtil.setCode("101");
                resultUtil.setError("没有接口访问权限，签名有误");
                return resultUtil;
            }
            if (StringUtils.isBlank(userName) || StringUtils.isBlank(mobile))
                throw new Exception("参数有误");
            TMxOrder tMxOrder = new TMxOrder();
            tMxOrder.setId(IdGen.uuid());
            tMxOrder.setUserName(userName);
            tMxOrder.setMobile(mobile);
            tMxOrder.setIsNewRecord(true);
            this.tMxOrderService.save(tMxOrder);
        } catch (Exception e) {
            this.logger.error("MxApiController addOrder error!", e);
            resultUtil.setCode(ResultUtil.ERROR_CODE);
            resultUtil.setError(e.getMessage());
        }
        return resultUtil;
    }

    @RequestMapping({"/test1"})
    @ResponseBody
    public ResultUtil test1(String data, HttpServletRequest request) {
        ResultUtil resultUtil = new ResultUtil(ResultUtil.SUCCESS_CODE);
        try {
            if (StringUtils.isBlank(data))
                throw new Exception("参数有误");
            System.out.println(data);
            data = MD5Util.decode(data, "ae9b787c1dba463a");
            if (data == null)
                throw new Exception("解密失败");
            resultUtil.putParams("data", data);
        } catch (Exception e) {
            this.logger.error("MxApiController url error!", e);
            resultUtil.setCode(ResultUtil.ERROR_CODE);
            resultUtil.setError(e.getMessage());
        }
        return resultUtil;
    }

    @RequestMapping({"/test/url"})
    public String urlTest(String data, HttpServletRequest request, Model model) throws Exception {
        try {
            if (StringUtils.isBlank(data))
                throw new Exception("参数有误");
            System.out.println(data);
            data = MD5Util.decode(data, "ae9b787c1dba463a");
            if (data == null)
                throw new Exception("解密失败");
            System.out.println(data);
            String[] params = data.split(";");
            String code = null;
            String type = null;
            String equityCode = "";
            for (String param : params) {
                String key = param.split("=")[0];
                String value = param.split("=")[1];
                if (key.equals("code"))
                    code = value;
                if (key.equals("type"))
                    type = value;
                if (key.equals("equityCode"))
                    equityCode = value;
                model.addAttribute(key, value);
            }
            if (code == null)
                throw new Exception("解密数据异常");
            MxSfj mxSfj = this.mxSfjDao.getByOrderNo(code, equityCode);
            if (mxSfj != null) {
                if (type.equals("1")) {
                    if (mxSfj.getOrderDateChu() != null)
                        model.addAttribute("orderDateChu", (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(mxSfj.getOrderDateChu()));
                    if (mxSfj.getOrderDateZhong() != null)
                        model.addAttribute("orderDateZhong", (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(mxSfj.getOrderDateZhong()));
                    if (mxSfj.getOrderDateMo() != null)
                        model.addAttribute("orderDateMo", (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(mxSfj.getOrderDateMo()));
                } else if (type.equals("2")) {
                    model.addAttribute("orderDate", (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(mxSfj.getOrderDate()));
                } else {

                }
                model.addAttribute("tenantId", mxSfj.getTenantId());
            } else {
                model.addAttribute("tenantId", "1");
            }
            if (type.equals("1")) {
                return "/error/404";
            } else if (type.equals("2")) {
                return "/error/404";
            } else if (type.equals("3")) {
                return "/mobile/modules/guoj/fzTest";
            } else if (type.equals("5")) {
                return "/mobile/modules/guoj/aiFacePageTest";
            } else if (type.equals("6")) {
                return "/mobile/modules/guoj/tonguePageTest";
            } else {
                return "/error/404";
            }
        } catch (Exception e) {
            this.logger.error("MxApiController url error!", e);
            return "/error/400";
        }
    }

    @RequestMapping({"/aiFacePage"})
    public String mxAiFacePage(Model model) {
        return "/mobile/modules/guoj/aiFacePage";
    }

    @RequestMapping({"/aiTonguePage"})
    public String mxAiTonguePage(Model model) {
        return "/mobile/modules/guoj/tonguePage";
    }

    @RequestMapping({"/url"})
    public String url(String data, HttpServletRequest request, Model model) throws Exception {
        try {
            if (StringUtils.isBlank(data))
                throw new Exception("参数有误");
            System.out.println(data);
            data = MD5Util.decode(data, "ae9b787c1dba463a");
            if (data == null)
                throw new Exception("解密失败");
            System.out.println(data);
            String[] params = data.split(";");
            String code = null;
            String type = null;
            String equityCode = "";
            for (String param : params) {
                String key = param.split("=")[0];
                String value = param.split("=")[1];
                if (key.equals("code"))
                    code = value;
                if (key.equals("type"))
                    type = value;
                if (key.equals("equityCode"))
                    equityCode = value;
                model.addAttribute(key, value);
                System.out.println("key:"+key+";value:"+value);
            }
            if (code == null)
                throw new Exception("解密数据异常");
            MxSfj mxSfj = this.mxSfjDao.getByOrderNo(code, equityCode);
            if (mxSfj != null) {
                if (type.equals("1")) {
                    if (mxSfj.getOrderDateChu() != null)
                        model.addAttribute("orderDateChu", (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(mxSfj.getOrderDateChu()));
                    if (mxSfj.getOrderDateZhong() != null)
                        model.addAttribute("orderDateZhong", (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(mxSfj.getOrderDateZhong()));
                    if (mxSfj.getOrderDateMo() != null)
                        model.addAttribute("orderDateMo", (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(mxSfj.getOrderDateMo()));
                } else if (type.equals("2")) {
                    model.addAttribute("orderDate", (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(mxSfj.getOrderDate()));
                } else {

                }
                model.addAttribute("tenantId", mxSfj.getTenantId());
            } else {
                model.addAttribute("tenantId", "1");
            }
            System.out.println(type);
            if (type.equals("1")) {
                return "/mobile/modules/guoj/sfj";
            } else if (type.equals("2")) {
                return "/mobile/modules/guoj/gfaj";
            } else if (type.equals("3")) {
                return "/mobile/modules/guoj/fz";
            }else if (type.equals("5")) {
                return "/mobile/modules/guoj/aiFacePage";
            }else if (type.equals("6")) {
                return "/mobile/modules/guoj/tonguePage";
            } else {
                return "/error/404";
            }
        } catch (Exception e) {
            this.logger.error("MxApiController url error!", e);
            return "/error/400";
        }
    }

    @RequestMapping({"/gfaj/save"})
    @ResponseBody
    public ResultUtil gfajSave(String code, String userName, String mobile, String userType, String tenantId, String tenantName, String orderDate, String equityCode, HttpServletRequest request) {
        ResultUtil resultUtil = new ResultUtil(ResultUtil.SUCCESS_CODE);
        try {
            if (StringUtils.isBlank(code))
                throw new Exception("参数有误");
            if (userType == null || !userType.equals("2"))
                throw new Exception("参数有误!");
            this.mxSfjService.gfajSave(code, userName, userType, mobile, tenantId, tenantName, orderDate, equityCode);
        } catch (Exception e) {
            this.logger.error("MxApiController gfajSave error!", e);
            resultUtil.setCode(ResultUtil.ERROR_CODE);
            resultUtil.setError(e.getMessage());
        }
        return resultUtil;
    }

    @RequestMapping({"/sfj/save"})
    @ResponseBody
    public ResultUtil sfjSave(String code, String userName, String mobile, String userType, String tenantId, String tenantName, String orderDateChu, String orderDateZhong, String orderDateMo, String equityCode, HttpServletRequest request) {
        ResultUtil resultUtil = new ResultUtil(ResultUtil.SUCCESS_CODE);
        try {
            if (StringUtils.isBlank(code))
                throw new Exception("参数有误");
            if (userType == null || !userType.equals("1"))
                throw new Exception("参数有误!");
            this.mxSfjService.sfjSave(code, userName, userType, mobile, tenantId, tenantName, orderDateChu, orderDateZhong, orderDateMo, equityCode);
        } catch (Exception e) {
            this.logger.error("MxApiController sfjSave error!", e);
            resultUtil.setCode(ResultUtil.ERROR_CODE);
            resultUtil.setError(e.getMessage());
        }
        return resultUtil;
    }

    @RequestMapping({"/test/aiFace/save"})
    @ResponseBody
    public ResultUtil aiFaceSaveTest(String code, String userName, String mobile, String userType, String equityCode, String tongue, String coating, HttpServletRequest request) {
        ResultUtil resultUtil = new ResultUtil(ResultUtil.SUCCESS_CODE);
        try {
            if (StringUtils.isBlank(code))
                throw new Exception("参数有误");
            if (userType == null || !userType.equals("5"))
                throw new Exception("参数有误!");
            this.mxSfjService.fzSaveTest(code, userName, userType, mobile, equityCode, tongue, coating);
        } catch (Exception e) {
            this.logger.error("MxApiController aiFaceSaveTest error!", e);
            resultUtil.setCode(ResultUtil.ERROR_CODE);
            resultUtil.setError(e.getMessage());
        }
        return resultUtil;
    }

    @RequestMapping({"/aiFace/save"})
    @ResponseBody
    public ResultUtil aiFaceSave(String code, String userName, String mobile, String userType, String equityCode, String tongue, String coating, HttpServletRequest request) {
        ResultUtil resultUtil = new ResultUtil(ResultUtil.SUCCESS_CODE);
        try {
            if (StringUtils.isBlank(code))
                throw new Exception("参数有误");
            if (userType == null || !userType.equals("5"))
                throw new Exception("参数有误!");
            this.mxSfjService.fzSave(code, userName, userType, mobile, equityCode, tongue, coating);
        } catch (Exception e) {
            this.logger.error("MxApiController aiFaceSave error!", e);
            resultUtil.setCode(ResultUtil.ERROR_CODE);
            resultUtil.setError(e.getMessage());
        }
        return resultUtil;
    }

    @RequestMapping({"/test/tongue/save"})
    @ResponseBody
    public ResultUtil tongueSaveTest(String code, String userName, String mobile, String userType, String equityCode, String tongue, String coating, HttpServletRequest request) {
        ResultUtil resultUtil = new ResultUtil(ResultUtil.SUCCESS_CODE);
        try {
            if (StringUtils.isBlank(code))
                throw new Exception("参数有误");
            if (userType == null || !userType.equals("6"))
                throw new Exception("参数有误!");
            this.mxSfjService.fzSaveTest(code, userName, userType, mobile, equityCode, tongue, coating);
        } catch (Exception e) {
            this.logger.error("MxApiController tongueSaveTest error!", e);
            resultUtil.setCode(ResultUtil.ERROR_CODE);
            resultUtil.setError(e.getMessage());
        }
        return resultUtil;
    }

    @RequestMapping({"/tongue/save"})
    @ResponseBody
    public ResultUtil tongueSave(String code, String userName, String mobile, String userType, String equityCode, String tongue, String coating, HttpServletRequest request) {
        ResultUtil resultUtil = new ResultUtil(ResultUtil.SUCCESS_CODE);
        try {
            if (StringUtils.isBlank(code))
                throw new Exception("参数有误");
            if (userType == null || !userType.equals("6"))
                throw new Exception("参数有误!");
            this.mxSfjService.fzSave(code, userName, userType, mobile, equityCode, tongue, coating);
        } catch (Exception e) {
            this.logger.error("MxApiController tongueSave error!", e);
            resultUtil.setCode(ResultUtil.ERROR_CODE);
            resultUtil.setError(e.getMessage());
        }
        return resultUtil;
    }


    @RequestMapping({"/test/fz/save"})
    @ResponseBody
    public ResultUtil fzSaveTest(String code, String userName, String mobile, String userType, String equityCode, String tongue, String coating, HttpServletRequest request) {
        ResultUtil resultUtil = new ResultUtil(ResultUtil.SUCCESS_CODE);
        try {
            if (StringUtils.isBlank(code))
                throw new Exception("参数有误");
            if (userType == null || !userType.equals("3"))
                throw new Exception("参数有误!");
            this.mxSfjService.fzSaveTest(code, userName, userType, mobile, equityCode, tongue, coating);
        } catch (Exception e) {
            this.logger.error("MxApiController gfajSave error!", e);
            resultUtil.setCode(ResultUtil.ERROR_CODE);
            resultUtil.setError(e.getMessage());
        }

        return resultUtil;
    }

    @RequestMapping({"/fz/save"})
    @ResponseBody
    public ResultUtil fzSave(String code, String userName, String mobile, String userType, String equityCode, String tongue, String coating, HttpServletRequest request) {
        ResultUtil resultUtil = new ResultUtil(ResultUtil.SUCCESS_CODE);
        try {
            if (StringUtils.isBlank(code))
                throw new Exception("参数有误");
            if (userType == null || !userType.equals("3"))
                throw new Exception("参数有误!");
            this.mxSfjService.fzSave(code, userName, userType, mobile, equityCode, tongue, coating);
        } catch (Exception e) {
            this.logger.error("MxApiController gfajSave error!", e);
            resultUtil.setCode(ResultUtil.ERROR_CODE);
            resultUtil.setError(e.getMessage());
        }
        return resultUtil;
    }
}
